package com.demo.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.reggie.common.BaseContext;
import com.demo.reggie.common.R;

import com.demo.reggie.dto.OrdersDto;
import com.demo.reggie.entity.Orders;
import com.demo.reggie.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrdersService ordersService;
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        ordersService.submit(orders);
        return R.success("支付成功");
    }
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String number,String beginTime,String endTime){
        Page<Orders> pageInfo=new Page(page,pageSize);
        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(number!=null,Orders::getNumber,number);
        queryWrapper.ge(beginTime!=null,Orders::getOrderTime,beginTime);
        queryWrapper.le(endTime!=null,Orders::getOrderTime,endTime);
        queryWrapper.orderByDesc(Orders::getCheckoutTime);
        ordersService.page(pageInfo,queryWrapper);

        List<Orders> records = pageInfo.getRecords();
        records = records.stream().map((item) -> {

            item.setUserName("用户" + item.getUserId());

            return item;
        }).collect(Collectors.toList());
        return R.success(pageInfo);

    }
    @PutMapping
    public R<String> updateOrder(@RequestBody Orders orders){
       LambdaUpdateWrapper<Orders> wrapper=new LambdaUpdateWrapper<>();
        wrapper.eq(orders.getId()!=null,Orders::getId,orders.getId());
        wrapper.set(Orders::getStatus,orders.getStatus());
        ordersService.update(wrapper);
        return R.success("派送成功");

    }
//    @GetMapping("/userPage")//简洁版
//    public R<Page> userPage(int page,int pageSize){
//        Page<OrdersDto> pageInfo=new Page<>(page,pageSize);
//        LambdaQueryWrapper<OrdersDto> queryWrapper=new LambdaQueryWrapper<>();
//        queryWrapper.eq(OrdersDto::getUserId, BaseContext.getCurrentId());
//        queryWrapper.orderByDesc(OrdersDto::getCheckoutTime);
//
//        ordersService.page(pageInfo,queryWrapper);
//        return R.success(pageInfo);
//    }

    @GetMapping("/userPage")
    public R<Page> userPage(int page,int pageSize) {
        Page pageInfo= ordersService.userPage(page,pageSize);

        return R.success(pageInfo);
    }
}
//http://localhost:8080/order/page?page=1&pageSize=10
//http://localhost:8080/order/page?page=1&pageSize=10&number=10&beginTime=2022-07-31 00:00:00&endTime=2022-09-04 23:59:59

